package java67_adv10;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySax extends DefaultHandler {
	public static final String XML_NAME = "src/java67_adv10/courses2.xml";

	public static void main(String[] args) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		parser.parse(XML_NAME, new MySax());
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.out.println("节点值:" + new String(ch, start, length).trim());
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		System.out.println("节点:" + qName + ",几个属性:" + attributes.getLength());
		for (int i = 0; i < attributes.getLength(); i++) {
			System.out.println(attributes.getQName(i) + ":" + attributes.getValue(attributes.getQName(i)));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println(qName + "元素结束");
	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("文档开始");
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("文档结束");
	}
}
